<template>
  <div>
    <r-search :searchHandle="searchHandleAdd" />
    <r-table
      :isSelection="true"
      :isPagination="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      @refresh="init()"
    ></r-table>
    <r-form
      :formRules="formRules"
      :formHandle="formHandle"
      :formData="formData"
      :formCols="formCols"
      ref="elForm"
      labelWidth="200px"
      dialogWidth="1000px"
      :isHandle="true"
    />
  </div>
</template>
<script>
export default {
  data() {
    var checkpass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("Enter Again PassWord"));
      } else if (value !== this.formDataEditPsw.password) {
        callback(new Error("The password is not the same!"));
      } else {
        callback();
      }
    };
    return {
      searchHandleAdd: [
        {
          label: "Add折扣",
          type: "success",
          icon: "el-icon-circle-plus-outline",
          handle: e => this.elFormVisible()
        }
      ],
      tableData: [],
      tableCols: [
        {
          label: "Start Time",
          prop: "startTime"
        },
        {
          label: "End Time",
          prop: "endTime"
          // theme: e => e.status ? 'primary' : 'danger', type: "tag",
        },
        {
          label: "折扣",
          prop: "discount"
        },
        {
          label: "Operations",
          type: "button",
          btnList: [
            {
              label: "Delete",
              type: "primary",
              handle: row => this.TableDel("/discountDel", { id: row.id })
            }
          ]
        }
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
      formData: {
        discount: ""
        // startDate: "",
        // endDate: "",
        // status: ""
      },
      formCols: [
        {
          type: "input",
          label: "折扣",
          placeholder: "Please Input折扣",
          width: 350,
          prop: "discount",
          remark: "折"
        },
        {
          type: "dateTime",
          label: "Start Time",
          placeholder: "Please select the start time",
          width: 350,
          prop: "startTime"
        },
        {
          type: "dateTime",
          label: "End Time",
          placeholder: "Please select the end time",
          width: 350,
          prop: "endTime"
        }
        // { type: "input", label: "折扣", placeholder: 'Please InputDevice Name', width: 350, prop: "discount", },
        // { type: "select", label: "Status", placeholder: 'Please SelectStatus', width: 350, prop: "status",options:[{label:'Enable',value:1},{label:'Forbidden',value:2}] },
      ],
      formRules: {
        discount: [
          { required: true, message: "Please Input折扣", trigger: "blur" }
        ],
        startTime: [
          {
            required: true,
            message: "Please select the start time",
            trigger: "blur"
          }
        ],
        endTime: [
          {
            required: true,
            message: "Please select the end time",
            trigger: "blur"
          }
        ]
        // discount: [{ required: true, message: 'Please InputDevice Name', trigger: 'blur' }],
      },
      formHandle: [
        {
          label: "Cancel",
          handle: e => this.elFormVisible()
        },
        {
          label: "Confirm",
          type: "success",
          handle: e => this.elFormSubmit()
        }
      ]
    };
  },
  components: {},
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.tablePage.offset = this.tablePage.page;
      let param = {
        ...this.searchData,
        ...this.tablePage,
        activeGoodsId: this.$route.query.id
      };
      // console.log(param);
      let res = await this.api.discount(param);
      if (res) {
        this.tablePage.total = res.data.total;
        this.tableData = res.data;
      }
    },
    async elFormSubmit() {
      this.formData.account = this.formData.phone;
      if (this.formData.id) {
      } else {
        let res = await this.api.discountadd({
          ...this.formData,
          activeId: this.$route.query.activeId * 1,
          activeGoodsId: this.$route.query.id
        });
        if (res) {
          this.elFormVisible();
          this.init();
        }
      }
    },
    elFormDetail(row) {
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = row[key];
      });
      this.formData.id = row.id;
      this.elFormVisible();
    }
  }
};
</script>
<style scoped lang="scss">
.el-icon-search {
  margin-right: 10px;
}
.Tip {
  margin-left: 20px;
}
</style>
